<template>
  <el-dialog title="详情" :close-on-click-modal="false" width="50%" :visible.sync="visible">
    <el-form :model="dataForm" label-width="200px" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()">
      <div class="row">
        <el-form-item label="环境编码" prop="environmentCode" class="col-md-10">
          <el-input v-model="dataForm.environmentCode" disabled placeholder="环境编码"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item label="URL" prop="environmentUrl" class="col-md-10">
          <el-input v-model="dataForm.environmentUrl" placeholder="URL"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item label="环境名称" prop="environmentName" class="col-md-10">
          <el-input v-model="dataForm.environmentName" placeholder="环境名称"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item label="是否有效" prop="isEffect" class="col-md-10">
          <el-switch v-model="dataForm.isEffect" active-color="#13ce66" inactive-color="#ff4949" on-value="1" on-text="是" off-text="否" off-value="0">
          </el-switch>
        </el-form-item>
      </div>
    </el-form>

    <!-- <span slot="footer" class="dialog-footer">
      <el-button type="danger" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">返回</el-button>
    </span> -->
    <span slot="footer" class="dialog-footer">
      <el-button :plain="true" type="info" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveData, getData } from '@/api/baseConfig/environment'

export default {
  data() {
    return {

      visible: false,
      dataForm: {
        id: null,
        environmentCode: '',
        environmentName: '',
        environmentUrl: '',
        remark: '',
        isEffect: '1'
      },

      dataRule: {
        environmentName: [
          { required: true, message: '环境名称不能为空', trigger: 'change' }
        ],
        environmentUrl: [
          { required: true, message: 'URL不能为空', trigger: 'change' }
        ]
      }

    }
  },
  created() {

  },
  methods: {

    //初始化
    init(id) {
      this.dataForm = {
        id: null,
        environmentCode: '',
        environmentName: '',
        environmentUrl: '',
        remark: '',
        isEffect: '1'
      }
      this.visible = true

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          this.dataForm.id = id
          let params = {
            id: this.dataForm.id + ''
          }
          this.$store.dispatch('loading/CHANGE_LOADING', true)
          getData(params).then(res => {
            this.$store.dispatch('loading/CHANGE_LOADING', false)
            if (res.data.data && res.data.resultCode === 0) {
              this.dataForm = res.data.data;
            }
          }).catch(err => {
            this.$store.dispatch('loading/CHANGE_LOADING', false)
          })
        }
      })
    },


    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$store.dispatch('loading/CHANGE_LOADING', true)
          saveData(JSON.stringify(this.dataForm)).then(res => {
            this.$store.dispatch('loading/CHANGE_LOADING', false)
            if (res.data && res.data.resultCode === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.dataForm = {
                    id: null,
                    id: null,
                    environmentCode: '',
                    environmentName: '',
                    environmentUrl: '',
                    remark: '',
                    isEffect: '1'
                  }
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.data.resultMsg)
            }
          }).catch(err => {
            this.$store.dispatch('loading/CHANGE_LOADING', false)
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.title {
  padding: 10px;
  background-color: rgba(248, 248, 248, 1);
  margin-bottom: 10px;
}

.el-table__row .el-form-item__content {
  margin-left: 0 !important;
}

.el-table__row .el-form-item {
  margin-bottom: 0 !important;
}
</style>
